<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/3/4
 * Time: 16:49
 */

namespace app\api\home;
use app\common\model\Recruit as recruitModel;
use app\common\model\RecruitClassify as classifyModel;
use app\api\validate\Recruit as validate;
class Recruit extends Base
{
    //招聘列表
    public function list(){
        $where      = $data = [];
        $page       = $this->request->param('page/d', 1);
        $limit      = $this->request->param('limit/d', 15);
        $keyword    = $this->request->param('keyword/s');

        if ($keyword) {
            $where[] = ['title', 'like', "%{$keyword}%"];
        }

        $data['data'] = recruitModel::with(['userinfo','user'])->where($where)->page($page)->limit($limit)->select();

        $data['count'] = recruitModel::where($where)->count('id');
        $data['code'] = 0;
        $data['msg'] = '';

        return $this->ajaxReturn($data);
    }

    //分类
    public function classify(){
        $where      = $data = [];
        $page       = $this->request->param('page/d', 1);
        $limit      = $this->request->param('limit/d', 15);
        $keyword    = $this->request->param('keyword/s');

        if ($keyword) {
            $where[] = ['name', 'like', "%{$keyword}%"];
        }

        $data['data'] = classifyModel::where($where)->page($page)->limit($limit)->select();

        $data['count'] = classifyModel::where($where)->count('id');
        $data['code'] = 0;
        $data['msg'] = '';

        return $this->ajaxReturn($data);
    }

    public function addRecruit(){
        //有效参数
        $where = ['uid','title','content','img','addr','classify'];
        $value = $this->request->only($where);

        $validate = new validate();

        if(!$validate->scene('add')->check($value)){
            return $this->ajaxError($validate->getError());
        }

        recruitModel::create($value,$where,true);

        return $this->ajaxReturn();
    }
}